/*
 * @Description: 个人中心   PersonalCenter
 * @version: 
 * @Company: 
 * @Author: ZhengZhuang
 * @Date: 2019-08-27 11:38:31
 * @LastAuthor   : zhengzhuang
 * @lastTime     : 2020-01-09 13:32:34
 */
import 'package:C20Driver/utils/utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:C20Driver/model/driver_information_model.dart';
import 'package:C20Driver/provider/driver_information.dart';
import 'package:C20Driver/routers/navigator_util.dart';
import 'package:C20Driver/service/request.dart';
import 'package:provider/provider.dart';

class PersonalCenter extends StatefulWidget {
  @override
  _PersonalCenterState createState() => _PersonalCenterState();
}

class _PersonalCenterState extends State<PersonalCenter> {
  String _result = "无";

  // 获取车辆信息
  @override
  void initState() {
    super.initState();
    Request.getSetDriverInfo(context);
    Request.geyMyCarInfo(context);
    Request.getDriverModifyStatus(context);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('个人中心'),
        elevation: 0,
        actions: <Widget>[
          InkWell(
            child: Padding(
              padding: const EdgeInsets.only(right: 16),
              child: Image.asset(
                'assets/icon/icon_linear_shezhi.png',
                width: ScreenUtil().setWidth(48),
                height: ScreenUtil().setWidth(48),
              ),
            ),
            onTap: () => NavigatorUtil.jump(context, '/appSetUp'),
          )
        ],
      ),
      body: Container(
        color: Color(0xffffffff),
        padding: EdgeInsets.all(ScreenUtil().setWidth(25.0)),
        child: Column(
          children: <Widget>[
            /// 个人资料
            _judgingRegistration(),
            SizedBox(
              height: ScreenUtil().setWidth(40),
            ),
            //我的行程
            _userCardListWidget(context, 'assets/icon/icon_fill_xingcheng.png',
                '我的行程', '/itineraryOrder?gotoUrl=intercity'),
            SizedBox(
              height: ScreenUtil().setWidth(32),
            ),
            //我的钱包
            _userCardListWidget(context, 'assets/icon/icon_fill_qianbao.png',
                '我的钱包', '/wallet'),
            SizedBox(
              height: ScreenUtil().setWidth(32),
            ),
            //邀请好友
            _userCardListWidget(context, 'assets/icon/icon_fill_tuandui.png',
                '邀请好友', '/inviteGoodFriends'),
            SizedBox(
              height: ScreenUtil().setWidth(32),
            ),

            /// 推广统计
            InkWell(
              child: Container(
                padding: EdgeInsets.all(ScreenUtil().setWidth(30.0)),
                decoration: BoxDecoration(
                    color: Color(0xffF6F8FC),
                    borderRadius: BorderRadius.all(
                        Radius.circular(ScreenUtil().setWidth(32)))),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    ClipOval(
                      child: Consumer<DriverInformation>(
                          builder: (context, state, child) {
                        return Image.asset(
                          'assets/icon/icon_fill_yunying.png',
                          width: ScreenUtil().setWidth(48.0),
                          height: ScreenUtil().setWidth(48.0),
                          fit: BoxFit.fill,
                        );
                      }),
                    ),
                    SizedBox(width: ScreenUtil().setWidth(20.0)),
                    Expanded(
                      child: Text(
                        '推广统计',
                        maxLines: 1,
                        style: TextStyle(
                          fontSize: ScreenUtil().setSp(26.0),
                          color: Color(0xff333333),
                        ),
                      ),
                    ),
                    Icon(
                      Icons.chevron_right,
                      color: Color(0xff9E9E9E),
                      size: ScreenUtil().setWidth(60.0),
                    ),
                  ],
                ),
              ),
              // onTap: () => NavigatorUtil.jumpWebView(context,
              //     "http://h5.tt.huashichuxing.com/promotionStatistics"),//测试
              onTap: () => NavigatorUtil.jumpWebView(context,
                  "http://c20h5.huashichuxing.com/promotionStatistics"), //正式
              // onTap: () => NavigatorUtil.jump(context,
              // "/confirmedAmount?orderId='123213'&sumCost=123123&startCost=123213&timeCost=12313&distanceCost=123123"),
            ),
            SizedBox(
              height: ScreenUtil().setWidth(32),
            ),
            //客服热线
            InkWell(
              child: Container(
                padding: EdgeInsets.all(ScreenUtil().setWidth(30.0)),
                decoration: BoxDecoration(
                    color: Color(0xffF6F8FC),
                    borderRadius: BorderRadius.all(
                        Radius.circular(ScreenUtil().setWidth(32)))),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    ClipOval(
                      child: Consumer<DriverInformation>(
                          builder: (context, state, child) {
                        return Image.asset(
                          'assets/icon/icon_fill_erji.png',
                          width: ScreenUtil().setWidth(48.0),
                          height: ScreenUtil().setWidth(48.0),
                          fit: BoxFit.fill,
                        );
                      }),
                    ),
                    SizedBox(width: ScreenUtil().setWidth(20.0)),
                    Expanded(
                      child: Text(
                        '客服热线',
                        maxLines: 1,
                        style: TextStyle(
                          fontSize: ScreenUtil().setSp(26.0),
                          color: Color(0xff333333),
                        ),
                      ),
                    ),
                    Icon(
                      Icons.chevron_right,
                      color: Color(0xff9E9E9E),
                      size: ScreenUtil().setWidth(60.0),
                    ),
                  ],
                ),
              ),
              onTap: () {
                Utils.callPhone('400-089-3089');
              },
            ),
            SizedBox(
              height: ScreenUtil().setWidth(32),
            ),
            //我的设置
            _userCardListWidget(context, 'assets/icon/icon_fill_shezhi_2.png',
                '我的设置', '/appSetUp'),
          ],
        ),
      ),
    );
  }

  // 判断用户是否注册成为司机车主--用户资料
  Widget _judgingRegistration() {
    return Consumer<DriverInformation>(builder: (context, state, child) {
      return Stack(
        children: <Widget>[
          Offstage(
            offstage: state.driverInfo.authenticationStatus != '0000',
            child: InkWell(
              child: _userCardWidget(context, state.driverInfo.driverPhone),
              onTap: () => NavigatorUtil.jump(context, '/personalData'),
              // onTap: () {},
            ),
          ),
          Offstage(
            offstage: state.driverInfo.authenticationStatus == '0000',
            child: InkWell(
              child: _userCardWidget(context, '去首页注册为C20出行车主 >'),
              onTap: () => NavigatorUtil.goBack(context),
            ),
          ),
        ],
      );
    });
  }

  // 车主用户资料卡片
  Widget _userCardWidget(context, desTxt) {
    return Container(
      padding: EdgeInsets.all(ScreenUtil().setWidth(30.0)),
      decoration: BoxDecoration(
          color: Color(0xffF6F8FC),
          borderRadius:
              BorderRadius.all(Radius.circular(ScreenUtil().setWidth(32)))),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          ClipOval(
            child:
                Consumer<DriverInformation>(builder: (context, state, child) {
              return Image.network(
                state.driverInfo.headimgurl != null
                    ? state.driverInfo.headimgurl
                    : '',
                width: ScreenUtil().setWidth(120.0),
                height: ScreenUtil().setWidth(120.0),
                fit: BoxFit.fill,
              );
            }),
          ),
          SizedBox(width: ScreenUtil().setWidth(20.0)),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Container(
                    width: ScreenUtil().setWidth(500.0),
                    child: Consumer<DriverInformation>(
                        builder: (context, state, child) {
                      return Text(
                        state.driverInfo.driverName != null
                            ? state.driverInfo.driverName
                            : '',
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                        style: TextStyle(
                          fontSize: ScreenUtil().setSp(40.0),
                          color: Colors.black,
                        ),
                      );
                    })),
                SizedBox(height: ScreenUtil().setWidth(10.0)),
                Text(
                  desTxt != null && desTxt.length > 0 ? desTxt : '',
                  maxLines: 1,
                  style: TextStyle(
                    fontSize: ScreenUtil().setSp(26.0),
                    color: Colors.black54,
                  ),
                ),
              ],
            ),
          ),
          Icon(
            Icons.chevron_right,
            color: Color(0xff9E9E9E),
            size: ScreenUtil().setWidth(60.0),
          ),
        ],
      ),
    );
  }

  Widget _userCardListWidget(
      context, String imageStr, String title, String jumpUrl) {
    return InkWell(
      child: Container(
        padding: EdgeInsets.all(ScreenUtil().setWidth(30.0)),
        decoration: BoxDecoration(
            color: Color(0xffF6F8FC),
            borderRadius:
                BorderRadius.all(Radius.circular(ScreenUtil().setWidth(32)))),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: <Widget>[
            ClipOval(
              child:
                  Consumer<DriverInformation>(builder: (context, state, child) {
                return Image.asset(
                  imageStr,
                  width: ScreenUtil().setWidth(48.0),
                  height: ScreenUtil().setWidth(48.0),
                  fit: BoxFit.fill,
                );
              }),
            ),
            SizedBox(width: ScreenUtil().setWidth(20.0)),
            Expanded(
              child: Text(
                title,
                maxLines: 1,
                style: TextStyle(
                  fontSize: ScreenUtil().setSp(26.0),
                  color: Color(0xff333333),
                ),
              ),
            ),
            Icon(
              Icons.chevron_right,
              color: Color(0xff9E9E9E),
              size: ScreenUtil().setWidth(60.0),
            ),
          ],
        ),
      ),
      onTap: () {
        if (jumpUrl.length > 0) {
          NavigatorUtil.jump(context, jumpUrl);
        }
      },
    );
  }

  // 功能区
  Widget _functionalArea() {
    return Card(
      child: Container(
        width: ScreenUtil().setWidth(700.0),
        child: Consumer<DriverInformation>(builder: (context, state, child) {
          return _judgingRegistrationFun(
              context, state.driverInfo.authenticationStatus);
        }),
      ),
    );
  }

  // 判断用户是否注册为车主--功能区
  Widget _judgingRegistrationFun(context, authenticationStatus) {
    if (authenticationStatus == '0000') {
      return Wrap(
        children: <Widget>[
          _functionOneArea(
              context, 'assets/icon/walletIcon.png', '钱包', '/wallet'),
          _functionOneArea(context, 'assets/icon/userTripIcon.png', '行程',
              '/itineraryOrder?gotoUrl=intercity'),
          _functionOneArea(context, 'assets/icon/userFlowingWater.png', '流水',
              '/myIncomeRecord'),
          _functionOneArea(context, 'assets/icon/userCustomerService.png', '客服',
              '/customerServiceCenter'),
          _functionOneArea(context, 'assets/icon/certification.png', '实名认证',
              '/realNameAuthentication'),
          _functionOneArea(context, 'assets/icon/user_share_icon.png', '邀请好友',
              '/inviteGoodFriends'),
        ],
      );
    } else {
      return Wrap(
        children: <Widget>[
          // _functionOneArea(context, 'assets/icon/hourglassIcon.png', '注册进度',
          //     '/weqweqwe'),
        ],
      );
    }
  }

  // 功能区单个小按钮
  Widget _functionOneArea(context, iconImage, title, jumpUrl) {
    return InkWell(
      child: Container(
        width: ScreenUtil().setWidth(170),
        height: ScreenUtil().setWidth(160.0),
        alignment: Alignment.center,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Image.asset(
              iconImage,
              width: ScreenUtil().setWidth(50.0),
              height: ScreenUtil().setWidth(50.0),
            ),
            SizedBox(
              height: ScreenUtil().setWidth(20.0),
            ),
            Text(
              title,
              style: TextStyle(
                fontSize: ScreenUtil().setSp(30.0),
                fontWeight: FontWeight.w400,
              ),
            ),
          ],
        ),
      ),
      onTap: () => NavigatorUtil.jump(context, jumpUrl),
    );
  }

  /// 我的车辆
  Widget _myVehicle() {
    return Consumer<DriverInformation>(builder: (context, state, child) {
      return Offstage(
        offstage: state.driverInfo.authenticationStatus != '0000',
        child: InkWell(
          child: Card(
            child: Container(
              padding: EdgeInsets.all(ScreenUtil().setWidth(30.0)),
              child: Row(
                children: <Widget>[
                  Expanded(
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: <Widget>[
                        Text(
                          '我的车辆',
                          style: TextStyle(
                            fontSize: ScreenUtil().setSp(30.0),
                          ),
                        ),
                        _vehicleInformationStatus(state.driverInfo),
                      ],
                    ),
                  ),
                  Icon(Icons.keyboard_arrow_right, color: Colors.black45),
                ],
              ),
            ),
          ),
          onTap: () => NavigatorUtil.jump(context, '/myVehicle'),
        ),
      );
    });
  }

  /// 我的列表
  Widget _myCenterList(String title) {
    return Consumer<DriverInformation>(builder: (context, state, child) {
      return Offstage(
        offstage: state.driverInfo.authenticationStatus != '0000',
        child: Card(
          child: Container(
            padding: EdgeInsets.all(ScreenUtil().setWidth(30.0)),
            child: Row(
              children: <Widget>[
                Expanded(
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: <Widget>[
                      Text(
                        '$title',
                        style: TextStyle(
                          fontSize: ScreenUtil().setSp(30.0),
                        ),
                      ),
                    ],
                  ),
                ),
                Icon(Icons.keyboard_arrow_right, color: Colors.black45),
              ],
            ),
          ),
        ),
      );
    });
  }

  // 司机车辆完善信息
  Widget _vehicleInformationStatus(DriverInformationModel driverInformation) {
    if (driverInformation.vehicleNumberPlate != '' &&
        driverInformation.vehicleNumberPlate != null) {
      return Text(
        driverInformation.vehicleNumberPlate,
        style: TextStyle(
          fontSize: ScreenUtil().setSp(30.0),
          color: Colors.black,
          fontWeight: FontWeight.w700,
        ),
      );
    } else {
      return Text(
        '待完善',
        style: TextStyle(
          fontSize: ScreenUtil().setSp(30.0),
          color: Color.fromARGB(255, 255, 153, 0),
          fontWeight: FontWeight.w700,
        ),
      );
    }
  }
}
